package com.distributer.order.model;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class Order implements Serializable {
	
    private static final long serialVersionUID = 1L;

    /**
    * order_id
    */
    private Integer orderId;

    /**
    * 订单号
    */
    private String orderNo;

    /**
    * 分销商编码
    */
    private String distributerNo;

    /**
    * 订单金额
    */
    private String orderAmount;

    /**
    * 实际支付金额
    */
    private String reallyPayAmount;

    /**
    * 订单状态
    */
    private Integer orderStatus;

    /**
    * 交易单号
    */
    private String tradeNo;
    
    private String productInfo;
    
    private String insuranceInfo;

    private static final Random RANDOM = new Random();
    
    public Order() {
    }
    
    public Order(String productInfo,String insuranceInfo) {
    	this.productInfo = productInfo;
    	this.insuranceInfo = insuranceInfo;
    	buildOrderInfo();	
    }
    
    public void buildOrderInfo(){
    	JSONObject productInfoJson = JSONObject.parseObject(productInfo);
    	this.orderNo = String.valueOf(System.currentTimeMillis()+RANDOM.nextInt(4));
    	this.distributerNo = productInfoJson.getString("distributerNo");
    	this.orderAmount = productInfoJson.getString("orderAmount");
    	this.reallyPayAmount = productInfoJson.getString("reallyPayAmount");
    	this.orderStatus = 5;
    }
    
	public Integer getOrderId() {
		return orderId;
	}

	public void setOrderId(Integer orderId) {
		this.orderId = orderId;
	}

	public String getOrderNo() {
		return orderNo;
	}

	public void setOrderNo(String orderNo) {
		this.orderNo = orderNo;
	}

	public String getDistributerNo() {
		return distributerNo;
	}

	public void setDistributerNo(String distributerNo) {
		this.distributerNo = distributerNo;
	}

	public String getOrderAmount() {
		return orderAmount;
	}

	public void setOrderAmount(String orderAmount) {
		this.orderAmount = orderAmount;
	}

	public String getReallyPayAmount() {
		return reallyPayAmount;
	}

	public void setReallyPayAmount(String reallyPayAmount) {
		this.reallyPayAmount = reallyPayAmount;
	}

	public Integer getOrderStatus() {
		return orderStatus;
	}

	public void setOrderStatus(Integer orderStatus) {
		this.orderStatus = orderStatus;
	}

	public String getTradeNo() {
		return tradeNo;
	}

	public void setTradeNo(String tradeNo) {
		this.tradeNo = tradeNo;
	}

	public String getProductInfo() {
		return productInfo;
	}

	public void setProductInfo(String productInfo) {
		this.productInfo = productInfo;
	}

	public String getInsuranceInfo() {
		return insuranceInfo;
	}

	public void setInsuranceInfo(String insuranceInfo) {
		this.insuranceInfo = insuranceInfo;
	}

	public List<OrderItem> getOrderItems() {
		JSONArray insuranceInfoJsons = JSONArray.parseArray(insuranceInfo);
		List<OrderItem> items = new LinkedList<OrderItem>();
		JSONObject productinfoJson = JSONObject.parseObject(productInfo);
		for (int i = 0; i < insuranceInfoJsons.size(); i++) {
			JSONObject currentInsuranceInfo = insuranceInfoJsons.getJSONObject(i);
			OrderItem orderItem = new OrderItem(this.orderNo,String.valueOf(this.orderNo+i));
			orderItem.buildProductinfo(productinfoJson)
			.buildPolicyInfo(currentInsuranceInfo);
			items.add(orderItem);
		}
		return items;
	}

	public static int getOrderStatusByOrderItemCreateResult(boolean orderItemCreateResult) {
		if (orderItemCreateResult) {
			return 10;
		}
		return 60;
	}

	public boolean statusIsVisible() {
		if (this.orderStatus == 5) {
			return false;
		}
		return true;
	}
}
